<template>
  <view>
    <my-goods :goods="goodsList" @click="gotoDetail" showSwipItem="true"></my-goods>
  </view>
</template>

<script>
  import {
    getGoodsList
  } from '@/utils/api.js'
  import {
    throttle
  } from '@/utils/tools.js'
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 5
        },
        goodsList: [],
        total: 0
      };
    },
    onLoad(option) {
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.query || ''
      this.getGoodsListEx()
    },
    methods: {
      async getGoodsListEx(cb) {
        const {
          data: res
        } = await getGoodsList({
          data: this.queryObj
        })
        if (res.meta.status !== 200) {
          this.$showMsg()
          return
        }
        cb && cb()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(e) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + e.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        this.$showMsg('数据加载完毕')
        return
      }
      this.queryObj.pagenum += 1
      throttle(this.getGoodsListEx, 1000)()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.goodsList = []

      this.getGoodsListEx(() => {
        uni.stopPullDownRefresh()
      })
    }
  }
</script>

<style lang="scss">

</style>